package coml.jiyun.hp.day02.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import java.util.ArrayList;
import java.util.List;
import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.RecyclerView;
import coml.jiyun.hp.day02.R;
import coml.jiyun.hp.day02.bean.Bean;
import coml.jiyun.hp.day02.fragment.CollectionFragment;

public class RlvMainAdapter extends RecyclerView.Adapter {

    private  Context mContext;
    public  ArrayList<Bean.ResultsBean> mList;

    public RlvMainAdapter(Context mContext, ArrayList<Bean.ResultsBean> mList) {
        this.mContext = mContext;
        this.mList = mList;
    }

    public RlvMainAdapter(Context context, CollectionFragment collectionFragment) {

    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.item_main, parent, false);
        return new VH(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        VH vh = (VH) holder;
        Bean.ResultsBean resultsBean = mList.get(position);
        RequestOptions requestOptions = new RequestOptions().placeholder(R.mipmap.ic_launcher);
        Glide.with(mContext).load(resultsBean.getUrl()).apply(requestOptions).into(vh.mIv);
        vh.mTv.setText(mList.get(position).getType());
        vh.itemView.setOnLongClickListener(new View.OnLongClickListener(){
            @Override
            public boolean onLongClick(View v) {
                if (itemLongClick !=null) {
                    itemLongClick.OnItemLongClick(v,position);
                }return false;
            }
        });
    }

    @Override
    public int getItemCount() {
        if (mList != null){
        return mList.size();

        }else {
            return 0;
        }
    }

    public void addAll(List<Bean.ResultsBean>resultsBeans){
        mList.addAll(resultsBeans);
        notifyDataSetChanged();
    }

    class VH extends RecyclerView.ViewHolder{

        private final TextView mTv;
        private final ImageView mIv;

        public VH(@NonNull View itemView) {
            super(itemView);
            mTv = itemView.findViewById(R.id.tv);
            mIv = itemView.findViewById(R.id.iv);
        }
    }
    public interface OnItemLongClick{
        void OnItemLongClick(View v,int position);
    }
    OnItemLongClick itemLongClick;

    public void setItemLongClick(OnItemLongClick itemLongClick) {
        this.itemLongClick = itemLongClick;
    }
}
